package com.yanqingshan.platform.module.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yanqingshan.platform.module.sys.mapper.SysUserRoleMapper;
import com.yanqingshan.platform.module.sys.model.domain.SysUserRole;
import com.yanqingshan.platform.module.sys.service.SysUserRoleService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 功能描述:
 *
 * @author yanqs
 * @since 2023-05-15
 */
@Service
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole> implements SysUserRoleService {
    /**
     * 用户取消权限
     *
     * @param userId  用户id
     * @param roleIds 角色ids
     * @return
     */
    @Override
    public boolean cancelRole(Long userId, List<Long> roleIds) {
        return remove(new LambdaQueryWrapper<SysUserRole>()
                .eq(SysUserRole::getUserId,userId).in(SysUserRole::getRoleId,roleIds));
    }

    /**
     * 用户分配权限
     *
     * @param userId  用户id
     * @param roleIds 角色Ids
     * @return
     */
    @Override
    public boolean grantSelectRole(Long userId, List<Long> roleIds) {
        List<SysUserRole> saveList = new ArrayList<>();
        for (Long id : roleIds) {
            SysUserRole userRole = new SysUserRole();
            userRole.setRoleId(id);
            userRole.setUserId(userId);
            saveList.add(userRole);
        }
        return saveBatch(saveList);
    }
}
